import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { CommentInfo } from './comment';

/*
  Generated class for the CommentDataProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class CommentDataProvider {
  private readonly baseUrl:string ='https://api2.bmob.cn/1/';
  private readonly httpHeaders:HttpHeaders=new HttpHeaders({
    'X-Bmob-Application-Id':'f68b5d0362c7c0522693b9685d255d9a',
    'X-Bmob-REST-API-Key':'0d3668e8eaeacc9de420e64b4f5415d7',
    'Content-Type':'application/json'
  });
  constructor(public http: HttpClient) {
   
  }
/**
 * 获取相关服务
 * @param objectId 
 */
  getCommentByParkId(objectId:string):Observable<any[]>{
    let whereStr=`{"parkid":{"__type":"Pointer","className":"park","objectId":"${objectId}"}}`;
    let _params=new HttpParams().set('where',whereStr)
    .set('include','parkid,userid')
    .set('order','-updatedAt');
    return this.http.get<any[]>(`${this.baseUrl}classes/comments`,
    {headers:this.httpHeaders,params:_params}).map(val=>val["results"]);
  }
  /**
   * 新增点评
   * @param commentInfo 
   */
  addComment(commentInfo:CommentInfo):Observable<CommentInfo>{
    let _commentInfo={
      content:commentInfo.content,
      userid: {
          __type:"Pointer",
          className:"_User",
          objectId:commentInfo.userid  
      },
      parkid: {
          __type:"Pointer",
          className:"park",
          objectId:commentInfo.parkid
      }
  }
  return this.http.post<CommentInfo>(`${this.baseUrl}classes/comments`,_commentInfo,{headers:this.httpHeaders});
  }
}
